<?php
//no direct access
defined('_JEXEC') or die( 'Restricted access' );
//import the JController Class
jimport( 'joomla.application.component.model' );


class NvModelWeather extends JModel {
	
	//var $tesURL = "http://localhost/joomladev/forecastrss.rss";
		
	function getWeather($keyword){
		
				
		$arrayKota = explode(" ",$keyword);
		
		if($arrayKota[0]=="Kab" || $arrayKota[0]=="Kota" ){
			
			$keyword = $arrayKota[1];
			
		}
		
				
		$wcode = $this->getWeatherCode($keyword);
				
	    $tesURL = "http://weather.yahooapis.com/forecastrss?p=".$wcode."&u=c";
	     
		//instantiate data array
		$data = array();
				
		$options['rssUrl'] = $tesURL;
		
		//parse rss object
		$parser =  JFactory::getXMLParser('rss',$options);
		
	
		//initiate data object array
		$data = array();
		$index = 0;
		
		//initiate $weather class as container for weather data
		$weather = new stdClass();
		
		//get <yweather:location> city data and store it as location
		$location = $parser->get_channel_tags('http://xml.weather.yahoo.com/ns/rss/1.0','location');
		$weather->location = $location[0]['attribs']['']['city'];
		
		
		//get link,title and image url and store it to an object array
		foreach ($parser->get_items(0,0) as $item){
				
			
			$weather->title = $item->get_title();
			$weather->link = $item->get_link();
			$weather->description = $item->get_description();
			
			$temperature = $item->get_item_tags('http://xml.weather.yahoo.com/ns/rss/1.0','condition');
						
			$weather->temperature  = $temperature[0]['attribs']['']['temp'];
			$weather->temperaturetext =  $temperature[0]['attribs']['']['text'];
			$weather->date =  $temperature[0]['attribs']['']['date'];
			
			
						 			
		}
		
		//store data into array
		$data[$index++] = $weather;
				 		
		return $data;
	}
	
	function getWeatherCode($keyword){
		
		$weatherCode;
		
		//lowered case the keyword
		$keylowered = strtolower($keyword);		if($keylowered == 'administrasi')			$keylowered = 'jakarta';		//echo $keylowered;
		
		
		$db =& JFactory::getDBO();
		//prepare the query
		 $query = 'select kodecuaca from'
		.$db->nameQuote('#__cuaca').
		' where nama='.$db->Quote($keylowered);
			
	     
		
		$db->setQuery($query);
		$weatherCode = $db->loadResult();
		
		
	
		return $weatherCode;
		
		
		
	}
	
	function getAllKota(){
		
		 $db =& JFactory::getDBO();
		//prepare the query
		 $query = "SELECT id,nama from #__cuaca";
		
		 $db->setQuery($query);
		 $rows = $db->loadObjectList();
		
		
		//data array for storing tourism object
		$data = array();
		$index = 0;
		
		foreach ($rows as $row){
			
			$data[$index++] = $row;
			
			
		}
		
		//mengembalikan data
		return $data;
		 	
	}
 
}
?>